<?php
class Zend_View_Helper_Search_Extended extends Zend_View_Helper_Abstract{
    private $_config;
    private $_request;
    
    public function __construct() {
        /**
         * @todo Переделать способ получения параметров, так как он может нести в себе потенциальную опасность
         */
        $this->_request = new \Lib\Data($_REQUEST);
    }
    private function _getParam($name, $default){
        if(isset($this->_request->$name) && null !== $this->_request->$name){
            return $this->_request->getValue($name);
        } else {
            return $default;
        }
    }
    public function search_Extended($request = null){
        $extendedSearchForm = new App_Form_Search_Extended();
        
        $stateId    = $this->_getParam('state',-1);
        $cityId     = $this->_getParam('city',-1);
    	$markId     = $this->_getParam('mark', -1);
        $modelId    = $this->_getParam('model', -1);
    	
/**
 * Categories
 **********************************************************************************/
        $categoryManager = new \Managers\Category();
        $categories = $categoryManager->getEnabled();
        $firstCategoryId = reset($categories)->getId();
        $categoryId = $this->_getParam('category', $firstCategoryId);
        
        $categoryId = ($categoryId == -1) ? $firstCategoryId : $categoryId;
        Lib_Utility::createMultiOptions($extendedSearchForm, $extendedSearchForm->getElement('category'), $categories);
        $extendedSearchForm->getElement('category')->setValue($categoryId);
        unset($categoryManager, $categories);
        
/**
 * States
 **********************************************************************************/
        
        // Adding states
    	$stateManager = new \Managers\State();
        $states = $stateManager->get(null);
        $placementSubform = $extendedSearchForm->getSubForm('placement')->getSubForm(0);
        Lib_Utility::createDefaultMultiOption($placementSubform, $placementSubform->getElement('state'), null, _('Выберите'));
        Lib_Utility::createMultiOptions($placementSubform, $placementSubform->getElement('state'),$states);
        $placementSubform->getElement('state')->setValue($stateId);
        unset($states, $stateManager);
/**
 * Cities
 **********************************************************************************/        
        // Adding cities
        $cityManager = null;
        Lib_Utility::createDefaultMultiOption($placementSubform,$placementSubform->getElement('city'), null, _('Выберите'));
        if(-1 !== $stateId){
            $cityManager = new \Managers\City();
            $cities = $cityManager->getByState($stateId, true);
            Lib_Utility::createMultiOptions($placementSubform, $placementSubform->getElement('city'),$cities);
            $placementSubform->getElement('city')->setValue($cityId);
        }
        
        unset($cities, $cityManager);        
/**
 * Marks
 **********************************************************************************/        
        // Adding mark selector
        $markManager = new \Managers\Mark();
        $marks = $markManager->getByCategory($categoryId);
        
        $generalSubform = $extendedSearchForm->getSubForm('general')->getSubForm(0);
        Lib_Utility::createDefaultMultiOption($generalSubform,$generalSubform->getElement('mark'), null, _('Выберите'));
        Lib_Utility::createMultiOptions($generalSubform,$generalSubform->getElement('mark'),$marks);
        $generalSubform->getElement('mark')->setValue($markId);
        
        unset ($marks, $markManager);
        
/**
 * Models
 **********************************************************************************/
        // Adding model selector
        Lib_Utility::createDefaultMultiOption($generalSubform,$generalSubform->getElement('model'), null, _('Выберите'));
        // Adding mark selector
        if($markId != -1){
            $modelManager = new \Managers\Model();
            $models = $modelManager->getByMark($markId);
            Lib_Utility::createMultiOptions($generalSubform, $generalSubform->getElement('model'), $models);
        }        
  
/**
 * Currencies
 **********************************************************************************/       
        $currencyManager = new \Managers\Currency();
        $currencies = $currencyManager->get(null);
        $priceSubform = $extendedSearchForm->getSubForm('price');
        Lib_Utility::createMultiOptions($priceSubform,$priceSubform->getElement('currency'),$currencies,'id','denotation');
        unset ($currencies, $currencyManager);
        
/**
 * Years
 **********************************************************************************/
        $yearSubform = $extendedSearchForm->getSubForm('year');
        $yearSetDesc = Lib_Utility::getRangeObjectSet(2010, 1980);
        $yearSetAsc = Lib_Utility::getRangeObjectSet(1980, 2010);
        
        Lib_Utility::createDefaultMultiOption($yearSubform,$yearSubform->getElement('from'), null, _('От'));
        Lib_Utility::createMultiOptions($yearSubform,$yearSubform->getElement('from'),$yearSetAsc);        
        Lib_Utility::createDefaultMultiOption($yearSubform,$yearSubform->getElement('to'), null, _('До'));
        Lib_Utility::createMultiOptions($yearSubform,$yearSubform->getElement('to'),$yearSetDesc);        
        
/**
 * Condition
 **********************************************************************************/
        $new = array(
            array('id'=>-1, 'name'=>'Все'),
            array('id'=>0, 'name'=>'Нормальное'),
            array('id'=>1, 'name'=>'На запчасти')
        );
        $new = Lib_Utility::createObjectSet($new);
        Lib_Utility::createMultiOptions($extendedSearchForm,$extendedSearchForm->getElement('condition'),$new);        

/**
 * Damage
 **********************************************************************************/
        $damage = array(
            array('id'=>-1, 'name'=>_('Все')),
            array('id'=>0, 'name'=>_('Без повреждений')),
            array('id'=>1, 'name'=>_('После ДТП'))
        );
        
        $damage = Lib_Utility::createObjectSet($damage);
        Lib_Utility::createMultiOptions($extendedSearchForm,$extendedSearchForm->getElement('damage'),$damage);
        
/**
 * Credit
 **********************************************************************************/        
        $credit = array(
            array('id'=>-1, 'name'=>_('Все')),
            array('id'=>0, 'name'=>_('Без кредита')),
            array('id'=>1, 'name'=>_('Взято в кредит'))
        );
        
        $credit = Lib_Utility::createObjectSet($credit);
        Lib_Utility::createMultiOptions($extendedSearchForm,$extendedSearchForm->getElement('credit'),$credit);
/**
 * Customed
 **********************************************************************************/      
        $custom = array(
            array('id'=>-1, 'name'=>_('Все')),
            array('id'=>0, 'name'=>_('Расстаможен')),
            array('id'=>1, 'name'=>_('Не расстаможен'))
        );
        
        $custom = Lib_Utility::createObjectSet($custom);
        Lib_Utility::createMultiOptions($extendedSearchForm,$extendedSearchForm->getElement('custom'),$custom);        

/**
 * Exchange types
 **********************************************************************************/
        $exchangeManager = new \Managers\ExchangeType();
        $exchange = $exchangeManager->get(null);
        Lib_Utility::createDefaultMultiOption($extendedSearchForm,$extendedSearchForm->getElement('exchange'), null, _('Без обмена'));
        Lib_Utility::createMultiOptions($extendedSearchForm,$extendedSearchForm->getElement('exchange'),$exchange);
        unset ($exchange, $exchangeManager);
        
/**
 * Colors
 **********************************************************************************/
        $colorManager = new \Managers\Color();
        $colors = $colorManager->get(null);
        Lib_Utility::createDefaultMultiOption($extendedSearchForm,$extendedSearchForm->getElement('color'), null, _('Все'));
        Lib_Utility::createMultiOptions($extendedSearchForm,$extendedSearchForm->getElement('color'),$colors);
        unset($colors, $colorManager);


/**
 * Gears
 **********************************************************************************/
        $gearManager = new \Managers\Gearbox();
        $gears = $gearManager->get(null);
        Lib_Utility::createDefaultMultiOption($extendedSearchForm,$extendedSearchForm->getElement('gear'), null, _('Все'));
        Lib_Utility::createMultiOptions($extendedSearchForm,$extendedSearchForm->getElement('gear'),$gears);
        unset($gears, $gearManager);
        
/**
 * Drives
 **********************************************************************************/
        $driveManager = new \Managers\Drive();
        $drives = $driveManager->get(null);
        Lib_Utility::createDefaultMultiOption($extendedSearchForm,$extendedSearchForm->getElement('drive'), null, _('Все'));
        Lib_Utility::createMultiOptions($extendedSearchForm,$extendedSearchForm->getElement('drive'),$drives);
        unset($drives, $driveManager);

/**
 * Fuels
 **********************************************************************************/
        $fuelManager = new \Managers\Fuel();
        $fuels = $fuelManager->get(null);
        Lib_Utility::createDefaultMultiOption($extendedSearchForm,$extendedSearchForm->getElement('fuel'), null, _('Все'));
        Lib_Utility::createMultiOptions($extendedSearchForm,$extendedSearchForm->getElement('fuel'),$fuels);
        unset($fuels, $fuelManager);
        
        
        return $extendedSearchForm;
    }
}